package com.wantong.jianpu.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.DeviceBean;
import com.wantong.jianpu.beans.FaultCommitBean;
import com.wantong.jianpu.beans.FaultDetailBean;
import com.wantong.jianpu.beans.FaultListBean;
import com.wantong.jianpu.beans.LocationBean;
import com.wantong.jianpu.beans.LoginBean;
import com.wantong.jianpu.beans.SelectImageBean;
import com.wantong.jianpu.beans.UpLoadBean;
import com.wantong.jianpu.event.FaultDetailEvent;
import com.wantong.jianpu.event.FaultListEvent;
import com.wantong.jianpu.models.FaultModel;
import com.wantong.jianpu.models.LocationModel;
import com.wantong.jianpu.models.UpLoadModel;
import com.wantong.jianpu.net.BaseObserver;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.DateUtil;
import me.goldze.mvvmhabit.utils.JsonUtil;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * @date 2022/4/25
 * description:
 */
public class FaultCreateViewModel extends BaseViewModel {
    public LocationModel locationModel;
    public FaultModel model;
    public UpLoadModel upLoadModel;

    public boolean isCreate=true;//是否是新建
    public String id;//如果是修改  原始数据id


    public SingleLiveEvent<String> commitText=new SingleLiveEvent<>();
    public SingleLiveEvent<String> location= new SingleLiveEvent<>();//位置信息

    public SingleLiveEvent<String> deviceName=new SingleLiveEvent<>();//设备名称
    public SingleLiveEvent<String[]> deviceEvent=new SingleLiveEvent<>();//设备弹窗
    public List<DeviceBean> deviceBeans;

    public SingleLiveEvent<List<SelectImageBean>> image=new SingleLiveEvent<>();//回显的图片


    public SingleLiveEvent<FaultCommitBean> commitEvent=new SingleLiveEvent<>();//提交数据


    /**
     * 位置信息
     */
    public List<LocationBean> locationBeans;
    public FaultCreateViewModel(@NonNull Application application) {
        super(application);
        locationModel=new LocationModel();
        model=new FaultModel();
        upLoadModel=new UpLoadModel();
        commitEvent.setValue(new FaultCommitBean());
        commitEvent.getValue().setFaultFiles(new ArrayList<>());
    }

    @Override
    protected void init() {
        super.init();
        getLocationRequest();
        if(isCreate){
            initDate();
            commitText.setValue("上报");
        }else{
            commitText.setValue("确认纠错");
            getDetailRequest();
        }
    }

    public void initDate() {
        commitEvent.getValue().setDaultDate(DateUtil.getNowDate(DateUtil.DatePattern.ALL_TIME));
    }


    /**
     * 设备选择
     */
    public void setDevicePosition(int position) {
        if (deviceBeans != null) {
            deviceName.setValue(deviceBeans.get(position).getEqubaseInfo().getEquName());
            commitEvent.getValue().setEquId(deviceBeans.get(position).getId());
        }
    }


    /**
     * 设备弹窗
     */
    public BindingCommand deviceDialog=new BindingCommand(() -> {
        if(commitEvent.getValue().getLocationId()==0){
            ToastUtils.show("请先选择故障位置");
            return;
        }
        showDialog();
        model.getEquListByLocation(commitEvent.getValue().getLocationId(), new BaseObserver<List<DeviceBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<DeviceBean>> t) {
                deviceBeans=t.getData();
                if (deviceBeans!= null) {
                    String[] brand = new String[deviceBeans.size()];
                    for (int i = 0; i < deviceBeans.size(); i++) {
                        brand[i] = deviceBeans.get(i).getEqubaseInfo().getEquName();
                    }
                    deviceEvent.setValue(brand);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    });


    /**
     * 上报故障提交
     */
    public BindingCommand commit=new BindingCommand(() -> {
        if(commitEvent.getValue().getLocationId()==0){
            ToastUtils.show("请选择故障位置");
            return;
        }
        if(StringUtils.isEmpty(commitEvent.getValue().getEquId())){
            ToastUtils.show("请选择故障设备");
            return;
        }
        if(StringUtils.isEmpty(commitEvent.getValue().getComments())){
            ToastUtils.show("请输入故障描述");
            return;
        }
        if(isCreate){
            commitRequest();
        }else{
            changeRequest();
        }
    });


    /**
     * 上报故障提交
     */
    public void commitRequest(){
        showDialog();
        model.commitRepair(JsonUtil.objectToJson(commitEvent.getValue()), new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                EventBus.getDefault().post(new FaultListEvent());
                ToastUtils.show(t.getMsg());
                finish();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }


    /**
     * 上报故障修改
     */
    public void changeRequest(){
        showDialog();
        model.changeRepair(JsonUtil.objectToJson(commitEvent.getValue()), new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                EventBus.getDefault().post(new FaultDetailEvent());
                ToastUtils.show(t.getMsg());
                finish();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }


    /**
     * 获取位置信息
     */
    private void getLocationRequest(){
        locationModel.location(new BaseObserver<List<LocationBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<LocationBean>> t) {
                locationBeans=t.getData();
            }
        },getLifecycleProvider());
    }


    /**
     * 上传文件
     * @param filePath
     */
    public void upLoadFile(List<String> filePath){
        showDialog();
        upLoadModel.upLoadImages(filePath, new BaseObserver<List<UpLoadBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<UpLoadBean>> t) {
                for (UpLoadBean datum : t.getData()) {
                    FaultCommitBean.FaultFilesDTO filesDTO=new FaultCommitBean.FaultFilesDTO();
                    filesDTO.setUrl(datum.getUrl());
                    filesDTO.setFileName(datum.getFileName());
                    commitEvent.getValue().getFaultFiles().add(filesDTO);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }


    /**
     * 查询报修详情
     */
    public void getDetailRequest(){
        showDialog();
        model.getRepairDetail(id, new BaseObserver<FaultDetailBean>() {
            @Override
            public void onSuccess(BaseResultBean<FaultDetailBean> t) {
                location.setValue(t.getData().getLocationName());
                deviceName.setValue(t.getData().getEquName());
                commitEvent.getValue().setId(t.getData().getId());
                commitEvent.getValue().setLocationId(t.getData().getLocationId());
                commitEvent.getValue().setDaultDate(t.getData().getDaultDate());
                commitEvent.getValue().setEquId(t.getData().getEquId());
                commitEvent.getValue().setComments(t.getData().getComments());
                commitEvent.getValue().setFaultFiles(t.getData().getFaultFiles());
                commitEvent.setValue(commitEvent.getValue());
                if(t.getData().getFaultFiles()!=null){
                    List<SelectImageBean> imageBeans=new ArrayList<>();
                    for (FaultCommitBean.FaultFilesDTO faultFile : t.getData().getFaultFiles()) {
                        SelectImageBean bean=new SelectImageBean();
                        bean.setFileName(faultFile.getFileName());
                        bean.setNet(true);
                        bean.setUrl(faultFile.getUrl());
                        imageBeans.add(bean);
                    }
                    image.setValue(imageBeans);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }

}
